﻿using Emgu.CV;
using Emgu.CV.Structure;
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder.Live;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PlayCardCapture
{
    public partial class SingleCapture : Form
    {
        private Capture capture;
        private List<Image<Bgr, Byte>> imageList;
        private int playerCount;
        private int cardPerPlayer;
        private bool incapture;
        private int frameCount;
        private int[] indexArray;
        private int capindex;
        private ImageManager manager;
        private ResultForm resultForm;
        private VideoWriter writer;
        private int stopCount;

        public SingleCapture()
        {
            InitializeComponent();
            capture = new Capture(0);
            capture.Grab();
            timer1.Start();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                if (capture != null)
                {
                    Image<Bgr, Byte> frame = capture.QueryFrame();
                    imageBox.Image = frame;
                    if (incapture)
                    {
                        //imageList.Add(frame.Clone());
                        writer.WriteFrame(frame);
                        frameCount++;
                        if(stopCount > 0)
                        {
                            if (stopCount > 10)
                            {
                                writer.Dispose();
                                manager = new SingleFileManager(@"record.avi", playerCount, cardPerPlayer, indexArray);
                                resultForm = new ResultForm();
                                resultForm.init(manager);
                                resultForm.Show();
                                incapture = false;
                                button1.Enabled = true;
                                button2.Enabled = false;
                                stopCount = 0;
                            }
                            else
                                stopCount++;
                        }
                    }
                }
        }

        private void SingleCapture_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (button2.Enabled)
            if (e.KeyChar == 'k')
            {
                if(capindex < playerCount * cardPerPlayer - 1)
                {
                    indexArray[capindex] = frameCount;
                    capindex++;
                }
                else if(capindex == playerCount * cardPerPlayer - 1)
                {
                    indexArray[capindex] = frameCount;
                    capindex++;
                    //manager = new SingleMemoryManager(imageList,playerCount,cardPerPlayer,frameCount,indexArray);
                    stopCount++;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            playerCount = int.Parse(comboBox1.Text);
            cardPerPlayer = int.Parse(comboBox2.Text);
            frameCount = 0;
            capindex = 0;
            indexArray = new int[playerCount * cardPerPlayer];
            //imageList = new List<Image<Bgr, byte>>();
            writer = new VideoWriter("record.avi", CvInvoke.CV_FOURCC('D', 'I', 'V', 'X'), 25, 720, 576, true);
            incapture = true;
            button1.Enabled = false;
            button2.Enabled = true;
            stopCount = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            incapture = false;
            button1.Enabled = true;
            button2.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (resultForm != null)
                resultForm.Show();
        }

        private void SingleCapture_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
